package com.lqk.nio.filechannel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception {
        // 创建文件的输入流
        File file = new File("D:\\Progrom Files\\java8new\\netty\\file\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过FileInputStream获取对应的FileChannel
        FileChannel fileChannel = fileInputStream.getChannel();

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        // 将通道中的数据读入到缓冲区
        fileChannel.read(byteBuffer);

        // 将byteBuffer中的数据转成String
        System.out.println(new String(byteBuffer.array()));

        fileInputStream.close();
    }
}
